import greenfoot.*;  

/**
 * The SandbagTarget actor is used to be randomly placed  somewhere on the world and check if the location is valid for dropping a sandbag
 * 
 * @author Jasper Jans
 */
public class SandbagTarget extends Actor
{
    private boolean confirmed = false;
    
    /*
     * Checks if the current coordinates are confirmed. If so, slowly fade the target marker from the screen
     */
    public void act() 
    {
        if(confirmed)
        {
            // Fade the target maker
            GreenfootImage img = getImage();
            int currentTrans = img.getTransparency();
            currentTrans = currentTrans - 10;
            img.setTransparency(currentTrans);
            setImage(img);
            
            // If visibility is almost zero remove me 
            if(getImage().getTransparency() <= 20)
            {
                getWorld().removeObject(this);
            }
        }  
    }    
    
    /*
     * Returns true if current coordinates are valid for dropping a sandbag.
     * This is done by checking if there are no Water (in range of 50) or Breach objects (in range of 100) nearby
     * Shows the target marker image when confirmed
     */
    public boolean confirmTarget()
    {
        if(getObjectsInRange(50, Water.class).size() == 0 && getObjectsInRange(100, Breach.class).size() == 0)
        {
            GreenfootImage image = getImage();
            image.setTransparency(255);
            setImage(image);
            confirmed = true;
            return true;
        }
        
        return false;
    }
}
